package com.robot.console.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;

public class FileHandler
{
	public static String PATH_SEP = System.getProperty("file.separator");
	public static String BACK_UP_FILE_SUFFIX = ".bak";

	public static String readFileToString(File file, boolean appendLine)
	{
		FileReader fileReader = null;
		LineNumberReader lr = null;
		String content = "";
		try
		{
			StringBuffer sb = new StringBuffer();
			fileReader = new FileReader(file);
			lr = new LineNumberReader(fileReader);
			String strLine = lr.readLine();
			while (strLine != null)
			{
				if (!strLine.equals(""))
				{
					sb.append(strLine);
					if (appendLine)
					{
						sb.append("\r\n");
					}
				}
				strLine = lr.readLine();
			}
			lr.close();
			fileReader.close();
			content = sb.toString();
		}
		catch (Exception ex)
		{
			content = "";
		}
		finally
		{
			if (fileReader != null)
			{
				try
				{
					fileReader.close();
				}
				catch (IOException e)
				{
				}
			}
			if (lr != null)
			{
				try
				{
					lr.close();
				}
				catch (IOException e)
				{
				}
			}
		}
		return content;
	}

	public static void backupFile(String source) throws Exception
	{
		copyFile(source, source + BACK_UP_FILE_SUFFIX);
	}

	public static void revertFile(String dest) throws Exception
	{
		copyFile(dest + BACK_UP_FILE_SUFFIX, dest);
	}

	public static void copyFile(String source, String dest) throws Exception
	{
		File in = new File(source);
		if (in.exists())
		{
			File out = new File(dest);
			File outParentFile = out.getParentFile();
			if (!outParentFile.exists())
			{
				outParentFile.mkdirs();
			}
			BufferedInputStream inFile = null;
			BufferedOutputStream outFile = null;
			try
			{
				inFile = new BufferedInputStream(new FileInputStream(in));
				outFile = new BufferedOutputStream(new FileOutputStream(out));
				byte[] buffer = new byte[8096];
				int i = 0;
				while ((i = inFile.read(buffer)) != -1)
				{
					outFile.write(buffer, 0, i);
				}
			}
			catch (Exception ex)
			{
			}
			finally
			{
				if (inFile != null)
				{
					try
					{
						inFile.close();
					}
					catch (IOException e)
					{
					}
				}
				if (outFile != null)
				{
					try
					{
						outFile.close();
					}
					catch (IOException e)
					{
					}
				}
			}
		}
	}

	public static void copyDict(String source, String dest) throws Exception
	{
		String source1;
		String dest1;
		File[] file = (new File(source)).listFiles();
		for (int i = 0; i < file.length; i++)
		{
			if (file[i].isFile())
			{
				source1 = source + PATH_SEP + file[i].getName();
				dest1 = dest + PATH_SEP + file[i].getName();
				copyFile(source1, dest1);
			}
		}
		for (int i = 0; i < file.length; i++)
		{
			if (file[i].isDirectory())
			{
				source1 = source + PATH_SEP + file[i].getName();
				dest1 = dest + PATH_SEP + file[i].getName();
				File dest2 = new File(dest1);
				dest2.mkdir();
				copyDict(source1, dest1);
			}
		}
	}

	public static void delFile(String source) throws Exception
	{
		File in = new File(source);
		if (in.exists())
		{
			in.delete();
		}
	}

	public static void makeDict(String source) throws Exception
	{
		File file = new File(source);
		if (!file.mkdir())
		{
			throw new Exception();
		}
	}

	public static void delDict(String source) throws Exception
	{
		String source1;
		File[] file = (new File(source)).listFiles();
		for (int i = 0; i < file.length; i++)
		{
			if (file[i].isFile())
			{
				source1 = source + PATH_SEP + file[i].getName();
				delFile(source1);
			}
		}
		for (int i = 0; i < file.length; i++)
		{
			if (file[i].isDirectory())
			{
				source1 = source + PATH_SEP + file[i].getName();
				delDict(source1);
			}
			file[i].delete();
		}
		new File(source).delete();
	}

	public static void writeContentToFile(String filename, String content) throws Exception
	{
		File file = new File(filename);
		writeContentToFile(file, content);
	}

	public static void writeContentToFile(File file, String content) throws Exception
	{
		if (file.exists() && file.isFile())
		{
			FileWriter fileWriter = null;
			try
			{
				fileWriter = new FileWriter(file);
				fileWriter.write(content);
				fileWriter.flush();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (fileWriter != null)
				{
					try
					{
						fileWriter.close();
					}
					catch (IOException e)
					{
					}
				}
			}
		}
	}
}
